package cn.com.generaldata.os.ws1.util;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.generaldata.os.pojo.mvc.Row;

public class DomUtil {

	private static Logger log = LoggerFactory.getLogger(DomUtil.class);

	@SuppressWarnings("unchecked")
	private static void createModel(Object entity, Element element) {
		Field[] fields = entity.getClass().getDeclaredFields();
		for (Field field : fields) {
			Object o;
			if (field.getName().equals("F1")) {
				o = ((Row) entity).getF1();
			} else if (field.getName().equals("F2")) {
				o = ((Row) entity).getF2();
			} else if (field.getName().equals("F3")) {
				o = ((Row) entity).getF3();
			} else if (field.getName().equals("F4")) {
				o = ((Row) entity).getF4();
			} else if (field.getName().equals("F5")) {
				o = ((Row) entity).getF5();
			} else if (field.getName().equals("F6")) {
				o = ((Row) entity).getF6();
			} else if (field.getName().equals("F7")) {
				o = ((Row) entity).getF7();
			} else if (field.getName().equals("F8")) {
				o = ((Row) entity).getF8();
			} else if (field.getName().equals("F9")) {
				o = ((Row) entity).getF9();
			} else if (field.getName().equals("F10")) {
				o = ((Row) entity).getF10();
			} else if (field.getName().equals("F11")) {
				o = ((Row) entity).getF11();
			} else if (field.getName().equals("F12")) {
				o = ((Row) entity).getF12();
			} else if (field.getName().equals("F13")) {
				o = ((Row) entity).getF13();
			} else if (field.getName().equals("F14")) {
				o = ((Row) entity).getF14();
			} else if (field.getName().equals("F15")) {
				o = ((Row) entity).getF15();
			} else if (field.getName().equals("F16")) {
				o = ((Row) entity).getF16();
			} else if (field.getName().equals("F17")) {
				o = ((Row) entity).getF17();
			} else if (field.getName().equals("F18")) {
				o = ((Row) entity).getF18();
			} else if (field.getName().equals("F19")) {
				o = ((Row) entity).getF19();
			} else if (field.getName().equals("F20")) {
				o = ((Row) entity).getF20();
			} else if (field.getName().equals("F21")) {
				o = ((Row) entity).getF21();
			} else if (field.getName().equals("F22")) {
				o = ((Row) entity).getF22();
			} else if (field.getName().equals("F23")) {
				o = ((Row) entity).getF23();
			} else if (field.getName().equals("F24")) {
				o = ((Row) entity).getF24();
			} else if (field.getName().equals("F25")) {
				o = ((Row) entity).getF25();
			} else if (field.getName().equals("F26")) {
				o = ((Row) entity).getF26();
			} else if (field.getName().equals("F27")) {
				o = ((Row) entity).getF27();
			} else if (field.getName().equals("F28")) {
				o = ((Row) entity).getF28();
			} else if (field.getName().equals("F29")) {
				o = ((Row) entity).getF29();
			} else if (field.getName().equals("F30")) {
				o = ((Row) entity).getF30();
			} else if (field.getName().equals("F31")) {
				o = ((Row) entity).getF31();
			} else if (field.getName().equals("F32")) {
				o = ((Row) entity).getF32();
			} else if (field.getName().equals("F33")) {
				o = ((Row) entity).getF33();
			} else if (field.getName().equals("F34")) {
				o = ((Row) entity).getF34();
			} else if (field.getName().equals("F35")) {
				o = ((Row) entity).getF35();
			} else if (field.getName().equals("F36")) {
				o = ((Row) entity).getF36();
			} else if (field.getName().equals("F37")) {
				o = ((Row) entity).getF37();
			} else if (field.getName().equals("F38")) {
				o = ((Row) entity).getF38();
			} else if (field.getName().equals("F39")) {
				o = ((Row) entity).getF39();
			} else if (field.getName().equals("F40")) {
				o = ((Row) entity).getF40();
			} else if (field.getName().equals("F41")) {
				o = ((Row) entity).getF41();
			} else if (field.getName().equals("F42")) {
				o = ((Row) entity).getF42();
			} else if (field.getName().equals("F43")) {
				o = ((Row) entity).getF43();
			} else if (field.getName().equals("F44")) {
				o = ((Row) entity).getF44();
			} else if (field.getName().equals("F45")) {
				o = ((Row) entity).getF45();
			} else if (field.getName().equals("F46")) {
				o = ((Row) entity).getF46();
			} else if (field.getName().equals("F47")) {
				o = ((Row) entity).getF47();
			} else if (field.getName().equals("F48")) {
				o = ((Row) entity).getF48();
			} else if (field.getName().equals("F49")) {
				o = ((Row) entity).getF49();
			} else if (field.getName().equals("F50")) {
				o = ((Row) entity).getF50();
			} else if (field.getName().equals("F51")) {
				o = ((Row) entity).getF51();
			} else if (field.getName().equals("F52")) {
				o = ((Row) entity).getF52();
			} else if (field.getName().equals("F53")) {
				o = ((Row) entity).getF53();
			} else if (field.getName().equals("F54")) {
				o = ((Row) entity).getF54();
			} else if (field.getName().equals("F55")) {
				o = ((Row) entity).getF55();
			} else if (field.getName().equals("F56")) {
				o = ((Row) entity).getF56();
			} else if (field.getName().equals("F57")) {
				o = ((Row) entity).getF57();
			} else if (field.getName().equals("F58")) {
				o = ((Row) entity).getF58();
			} else if (field.getName().equals("F59")) {
				o = ((Row) entity).getF59();
			} else if (field.getName().equals("F60")) {
				o = ((Row) entity).getF60();
			} else if (field.getName().equals("serialVersionUID")) {
				o = "sss";
			} else {
				try {
					o = PropertyUtils.getProperty(entity, field.getName());
					if (o != null) {
						//
						if (o instanceof String) {
							String fieldName = field.getName().equals("id")
									&& (entity instanceof cn.com.generaldata.os.pojo.ReportDetail) ? "rowId" : field
									.getName();
							Element childElement = element.addElement(fieldName);
							childElement.setText(o.toString());
						} else if (o instanceof List) {
							Element childElement = element.addElement(field.getName());
							Iterator it = ((ArrayList) o).iterator();
							while (it.hasNext()) {
								String grandsonName = field.getName();
								grandsonName = grandsonName.substring(0, grandsonName.length() - 1);
								Element grandsonElement = childElement.addElement(grandsonName);
								createModel(it.next(), grandsonElement);
							}
						} else if (o instanceof Set) {
							String fieldName = field.getName().equals("reportDetails") ? "rows" : field.getName();
							Element childElement = element.addElement(fieldName);
							if (field.getName().equals("reportDetails")) {

								Iterator it = ((Set) o).iterator();
								while (it.hasNext()) {

									String grandsonName = fieldName;
									grandsonName = grandsonName.substring(0, grandsonName.length() - 1);
									Element grandsonElement = childElement.addElement(grandsonName);
									createModel(it.next(), grandsonElement);
								}
							}

						} else if (o instanceof java.util.Date) {
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
							String str = sdf.format(o);
							Element childElement = element.addElement(field.getName());
							childElement.setText(str);
						} else if (o instanceof java.lang.Byte) {
							String s = ((java.lang.Byte) o).toString();
							Element childElement = element.addElement(field.getName());
							childElement.setText(s);
						} else if (o instanceof java.lang.Short) {
							Element childElement = element.addElement(field.getName());
							childElement.setText(o.toString());
						} else if (o instanceof java.lang.Integer) {
							Element childElement = element.addElement(field.getName());
							childElement.setText(o.toString());
						} else {
						}
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					log.error("生成报表错误", e);
				}
			}
		}
	}

	/**
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	public static String getOrgbw(Object entity, String serviceId) {
		String xml = "";
		Document document = DocumentHelper.createDocument();
		document.setXMLEncoding("GBK");
		Element element = document.addElement("report");
		createModel(entity, element);
		xml = document.asXML();
		return xml;

	}
}
